<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


/*******************
***  系统内部消息提醒类
***  
***
**********/
class Message
{

 
	public $message_id = null;
   

    public function __construct()
    {
        $this->CI =& get_instance();
		$this->CI->load->database();
		$this->messageType = array(
			"0"=>'【消息提醒】',	//销售提交海外仓库存申请后提醒
			"1"=>'【海外仓】申请库存',	//销售提交海外仓库存申请后提醒
			"2"=>'【海外仓】计划一审',	//生成运输任务之后的一审人员
			"3"=>'【海外仓】计划二审',	//运输任务二审人员
			"4"=>'【海外仓】封箱发货',	//运输任务二审之后的封箱人员
			"5"=>'【新品开发】初审',	//新品开发初审
			"6"=>'【新品开发】复审',	//【新品开发】复审
			"7"=>'【新品开发】出图',
			"8"=>'【海外仓】撤单',     //销售作废海外仓订单，撤单失败时产生的消息提醒
			"9"=>'【商品状态调整】',
			"10"=>'【工厂订单】',
		);

    }
	
	public function getMessageType($id=0)
	{
		return  $this->messageType[$id];
	
	}
	
	 /**
	  +----------------------------------------------------------
	 * 自动消息,清仓，下架，停售，重新销售，改价等
	  +----------------------------------------------------------
	 * @param array       $message  消息内容
	  +----------------------------------------------------------
	 * @return string
	  +----------------------------------------------------------
	 */
	public function sysstatisticalMessage()
	{
		$Userlist = $this->CI->fun->getdatabaseslist("d_user"," AND (classid in(3,2,4,5,6,8) or id = 295 ) AND type = 1","name,id");
		//$Userlist = $this->CI->db->select('name')->get_where('d_user',array('id'=>295,'type'=>1))->result_array();
		//停售
		$rsdb = $this->CI->db->get_one("SELECT count(*) as count FROM db_sys_workbench WHERE `types` = 4 AND istips = 0");
		if($rsdb["count"]>0){
			foreach($Userlist as $rs)
			{
				$randid = $this->getMessageNums();	//获取当前存储的条数
				$postData = "";
				$postData["message_type"] = "【商品停售（平台下架）】";	//商品状态调整
				$message = "截止[".date("Y-m-d H:i:s")."],系统共有 ".$rsdb["count"]." 条商品停售信息。请及时从销售平台下架商品！请前往停售商品信息中查看详细SKU";
				$postData["message"] = $message;
				$postData["message_toName"] = $rs['name'];
				$postData["isread"] =  $randid;
				$postData["url"] = "/welcome/ajaxbutton?op=showMoreGoods&types=4&times=".date("Y-m-d");
				$postData["message_toID"] = $rs["id"]?$rs["id"]:295;	//接收对象ID
				$postData["message_formID"] = 0;
				$postData["message_formName"] = 'SYSYTEM';
				$this->saveMessage($postData);
			}
			//将今日提示关闭
			$this->CI->db->update("db_sys_workbench",array("istips"=>time()),"types=4 AND istips = 0");
		}

		//清仓
		$rsdb = $this->CI->db->get_one("SELECT count(*) as count FROM db_sys_workbench WHERE `types` = 5 AND istips = 0");
		if($rsdb["count"]>0){
			foreach($Userlist as $rs)
			{
				$randid = $this->getMessageNums();	//获取当前存储的条数
				$postData = "";
				$postData["message_type"] = "【清仓商品信息】";	//商品状态调整
				$message = "截止[".date("Y-m-d H:i:s")."],系统共有 ".$rsdb["count"]." 条商品设置了折扣清仓。请及时更新销售平台信息！请前往系统今日信息中查看详细SKU";
				$postData["message"] = $message;
				$postData["message_toName"] = $rs['name'];
				$postData["isread"] =  $randid;
				$postData["url"] = "/welcome/ajaxbutton?op=showMoreGoods&types=5&times=".date("Y-m-d");
				$postData["message_toID"] = $rs["id"]?$rs["id"]:295;	//接收对象ID
				$postData["message_formID"] = 0;
				$postData["message_formName"] = 'SYSYTEM';
				$this->saveMessage($postData);
			}
			$this->CI->db->update("db_sys_workbench",array("istips"=>time()),"types=5 AND istips = 0");
		}

		//暂停销售
		$rsdb = $this->CI->db->get_one("SELECT count(*) as count FROM db_sys_workbench WHERE `types` = 6 AND istips = 0");
		if($rsdb["count"]>0){
			foreach($Userlist as $rs)
			{
				$randid = $this->getMessageNums();	//获取当前存储的条数
				$postData = "";
				$postData["message_type"] = "【商品暂停销售】";	//商品状态调整
				$message = "截止[".date("Y-m-d H:i:s")."],系统共有 ".$rsdb["count"]." 条商品设置了暂停销售。请及时更新销售平台信息！请前往系统今日信息中查看详细SKU";
				$postData["message"] = $message;
				$postData["message_toName"] = $rs['name'];
				$postData["isread"] =  $randid;
				$postData["url"] = "/welcome/ajaxbutton?op=showMoreGoods&types=6&times=".date("Y-m-d");
				$postData["message_toID"] = $rs["id"]?$rs["id"]:295;	//接收对象ID
				$postData["message_formID"] = 0;
				$postData["message_formName"] = 'SYSYTEM';
				$this->saveMessage($postData);
			}
			$this->CI->db->update("db_sys_workbench",array("istips"=>time()),"types=6 AND istips = 0");
		}

		//正常销售
		$rsdb = $this->CI->db->get_one("SELECT count(*) as count FROM db_sys_workbench WHERE `types` = 2 AND istips = 0");
		if($rsdb["count"]>0){
			foreach($Userlist as $rs)
			{
				$randid = $this->getMessageNums();	//获取当前存储的条数
				$postData = "";
				$postData["message_type"] = "【商品正常销售】";	//商品状态调整
				$message = "截止[".date("Y-m-d H:i:s")."],系统共有 ".$rsdb["count"]." 条商品从暂停销售改成正常销售。请及时更新销售平台信息！请前往系统今日信息中查看详细SKU";
				$postData["message"] = $message;
				$postData["message_toName"] = $rs['name'];
				$postData["isread"] =  $randid;
				$postData["url"] = "/welcome/ajaxbutton?op=showMoreGoods&types=2&times=".date("Y-m-d");
				$postData["message_toID"] = $rs["id"]?$rs["id"]:295;	//接收对象ID
				$postData["message_formID"] = 0;
				$postData["message_formName"] = 'SYSYTEM';
				$this->saveMessage($postData);
			}
			$this->CI->db->update("db_sys_workbench",array("istips"=>time()),"types=2 AND istips = 0");
		}

		//修改成本价
		$rsdb = $this->CI->db->get_one("SELECT count(*) as count FROM db_sys_workbench WHERE `types` = 9 AND istips = 0");
		if($rsdb["count"]>0){
			foreach($Userlist as $rs)
			{
				$randid = $this->getMessageNums();	//获取当前存储的条数
				$postData = "";
				$postData["message_type"] = "【商品修改成本价】";	//商品状态调整
				$message = "截止[".date("Y-m-d H:i:s")."],系统共有 ".$rsdb["count"]." 条商品修改成本价。请及时更新销售平台信息！请前往系统今日信息中查看详细SKU";
				$postData["message"] = $message;
				$postData["message_toName"] = $rs['name'];
				$postData["isread"] =  $randid;
				$postData["url"] = "/welcome/ajaxbutton?op=showMoreGoods&types=9&times=".date("Y-m-d");
				$postData["message_toID"] = $rs["id"]?$rs["id"]:295;	//接收对象ID
				$postData["message_formID"] = 0;
				$postData["message_formName"] = 'SYSYTEM';
				$this->saveMessage($postData);
			}
			$this->CI->db->update("db_sys_workbench",array("istips"=>time()),"types=9 AND istips = 0");
		}

		//修改关联价
		$rsdb = $this->CI->db->get_one("SELECT count(*) as count FROM db_sys_workbench WHERE `types` = 10 AND istips = 0");
		if($rsdb["count"]>0){
			foreach($Userlist as $rs)
			{
				$randid = $this->getMessageNums();	//获取当前存储的条数
				$postData = "";
				$postData["message_type"] = "【商品修改关联价】";	//商品状态调整
				$message = "截止[".date("Y-m-d H:i:s")."],系统共有 ".$rsdb["count"]." 条商品修改关联价。请及时更新销售平台信息！请前往系统今日信息中查看详细SKU";
				$postData["message"] = $message;
				$postData["message_toName"] = $rs['name'];
				$postData["isread"] =  $randid;
				$postData["url"] = "/welcome/ajaxbutton?op=showMoreGoods&types=10&times=".date("Y-m-d");
				$postData["message_toID"] = $rs["id"]?$rs["id"]:295;	//接收对象ID
				$postData["message_formID"] = 0;
				$postData["message_formName"] = 'SYSYTEM';
				$this->saveMessage($postData);
			}
			$this->CI->db->update("db_sys_workbench",array("istips"=>time()),"types=10 AND istips = 0");
		}
		
	
	}
    
	public function saveMessage($postData)
	{
		$postMessageData = $postData;
		$postMessageData["message"] = $this->myTrim($postData["message"]);
		$postMessageData["instime"] = time();
		 $res = $this->CI->db->query($this->CI->db->insert_string('db_message', $postMessageData));
		 return $this->CI->db->insert_id();
	}
	
	public function myTrim($str)
	{
		$search = array(" ","　","\n","\r","\t","'"," ");
		$replace = array("","","","","","","");
		$str=str_replace($search, $replace, $str);
		return preg_replace("/[\s\v".chr(194).chr(160)."]+$/","",$str); 
	}
	public function getMessageNums()
	{
		$rsdb = $this->CI->db->get_one("SELECT count(*) as count FROM db_message WHERE 1");
		$nums = $rsdb["count"];
		return $nums;
	}

	public function getMessageslist($userid)
	{
		$newmessage = "";
		$where = " (message_toID='".$userid."' or message_toID='9999') AND isshow =0";
		$this->CI->db->where($where);
		$messagelist = $this->CI->db->get('db_message')->result_array();
		if(count($messagelist)>0)
		{
			foreach($messagelist as $key=>$rs)
			{
				$rsdb = $this->CI->db->get_one("SELECT * FROM db_message_read WHERE message_id ='".$rs["id"]."' AND isread = 1 AND  userid='".$userid."'");
				if(!$rsdb)
				{
					$newmessage[] = $rs;
				}
			}
			return array("messageslist"=>$newmessage,"messagetotal"=>count($newmessage));
		}else{
			return false;
		}
	}

	public function SendMail($subject,$message,$to,$cc='',$test=0)
	{
		//$this->load->library('email');
		$this->CI->email->clear();
		$config['protocol'] = 'smtp'; 
        $config['smtp_host'] = 'ssl://smtp.exmail.qq.com'; 
        $config['smtp_user'] = 'yg@fumann.com'; 
        $config['smtp_pass'] = '113632cN'; 
        $config['smtp_port'] = '465'; 
        $config['smtp_timeout'] = '5'; 
        $config['newline'] = "\r\n"; 
        $config['crlf'] = "\r\n"; 
        $this->CI->email->initialize($config); 
		$this->CI->email->from('yg@fumann.com'); 
		if($test == 1)
		{
			$this->CI->email->to("46952407@qq.com"); 
		}else{
			$this->CI->email->to($to);
			if($cc)$this->CI->email->cc($cc);
		}
        $this->CI->email->subject(trim($subject)); 
        $this->CI->email->message($message); 
        $this->CI->email->send(); 
	}


}